package stsj.meddroid.screen;

import static stsj.meddroid.Resources.VP_HEIGHT;
import static stsj.meddroid.Resources.VP_WIDTH;
import stsj.meddroid.MainAppListener;
import stsj.meddroid.Resources;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;



public class CreditScreen implements Screen, InputProcessor {

	private SpriteBatch batch;
	private Camera camera;

	private MainAppListener mainListener;

	int x;
	int y;
	
	private static final int leftBorder = 240;
	private static final int rightBorder = 1955 + 1653 - 250;
	private int scrollingSpeed  = 1;
	private boolean moving;
	
	private int x1;
	private int x2;
	private int x3;
	private int x4;

	public CreditScreen(MainAppListener mainListener) {
		// TODO Auto-generated constructor stub
		this.mainListener = mainListener;
		camera = Resources.camera;
		batch = Resources.batch;
		x = VP_WIDTH / 2;
		y = VP_HEIGHT / 2;
		camera.position.set(240	, 160, 0);
		

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		Resources.stopAllMusics();
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void render(float arg0) {
		// TODO Auto-generated method stub
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		if (moving){
			if (x <= rightBorder){
				x+=scrollingSpeed;
			}else{
				x = leftBorder;
			}
		}
		
			if (x + VP_WIDTH /2 >rightBorder){
				x  = leftBorder;
			}
			else if (x  <leftBorder){
				x = leftBorder;
			}
		
		camera.position.set(x,y,0);
		camera.update();
		batch.setProjectionMatrix(camera.combined);

		batch.disableBlending();
		batch.begin();
		batch.draw(Resources.credit1, 0, 0);
		batch.draw(Resources.credit2, 1653, 0);
		
		
		batch.end();
		
		batch.enableBlending();
		
		
	}

	@Override
	public void resize(int arg0, int arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		Gdx.input.setInputProcessor(this);
		x = leftBorder;
		moving = true;
	}

	@Override
	public boolean keyDown(int arg0) {
		// TODO Auto-generated method stub
		

		return false;
	}

	private void backToMainMenu() {
		// TODO Auto-generated method stub
		mainListener.setState(MainAppListener.MAIN_MENU);
	}

	@Override
	public boolean keyTyped(char arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean keyUp(int arg0) {
		// TODO Auto-generated method stub
		if (arg0 == Input.Keys.BACK) {
			Resources.playSound(Resources.backSound);
			backToMainMenu();
		} 
		return false;
	}

	@Override
	public boolean scrolled(int arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchDown(int arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		x1 = arg0;
		x4 = x1;
		return false;
	}

	@Override
	public boolean touchDragged(int arg0, int arg1, int arg2) {

		moving = false;		
		x3 = x4;
		x4 = arg0;
		if (x4 > x3){
			x -= (x4-x3);
		}else{
			x += (x3-x4);
		}
		
		return false;
	}

	@Override
	public boolean touchMoved(int arg0, int arg1) {
		// TODO Auto-generated method stub
		
		return false;
	}

	@Override
	public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		moving = true;
		x2 = arg0;
		x3 = x2;
//		if (x2 > x1){
//			if (x - VP_WIDTH /  2 >= rightBorder +10){
//			}else{
//			}
//		}else{
//			if (x + VP_WIDTH /  2 <= leftBorder  -10){
//				
//			}else{
//			}
//			
//		}

		return false;
	}

}
